<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
	<head> 
    <meta charset="UTF-8">
    <%@ include file="/WEB-INF/views/include/head.jsp"%>
    <link rel="stylesheet" href="${ctxStatic}/css/common_erp.css">
    <link rel="stylesheet" href="${ctxStatic}/css/layerSm.css"> 
    <script type="text/javascript">
    
    $(document).ready(function(){
        $("#backBtn").click(function(){
            $("#showHtml").removeClass("showHtml");
            $("#confirmPage").css("display","none");
            $("#editPage").css("display","block");
            $("#showSubItemConfirm").html("&nbsp;");
            $("#showSubDiv").show();
            return false;
        });
    });
      // 正则表达式，判断0到9字母a到z下划线_
      var lettersnumbers=/^[0-9a-zA-Z_\s?]*$/;
      
      //保存方法
      var checkForm={
          t:null,
          check:function(t){
              $(t).attr("disabled",true);
              this.t=t;
              // 表单验证
              $("#inputForm").validate({
                  // 校验是否重名
                  rules: {
                   itemValue: {remote: "${ctx}/material/category/checkName?oldItemValue=" +$("#oldItemValue").val() },
                  }, 
                  messages: {
                  //分类值已存在
                  itemValue: {remote: '<spring:message code="categoryItemValue_exist"/>'},
                  },
                  fail: function(element,e) {
                  	$(checkForm.t).attr("disabled",false);
                  },
                  submitHandler: function(form) {  
	                  $("#showHtml").addClass("showHtml");
	                  $("#confirmPage").css("display","block");
	                  $("#editPage").css("display","none");
	                  $("#showSubItemConfirm").text($("#showSubItemEdit").text());
	                  $("#showSubDiv").hide();
	                  $(checkForm.t).attr("disabled",false);
                  } 
              });
              
              // 提交表单
              $("#inputForm").submit(); 
          }
      }
      
      //保存方法
      var saveForm={
          s:null,
          t:null,
          save:function(s,t){
              $(t).attr("disabled",true);
              this.s=s;
              this.t=t;
              // 从表单中获取数据
              var params = $("#inputForm").serialize();
              // 通过调用ajax 方法调用后台方法
              $.post("${ctx}/material/category/save",params,function(result){
                  // 提示信息
                  tipParent(result.message);
                  // 返回状态
                  if(result.statusCode==STATUSCODEOK){
                      if(saveForm.s==1){
                          // 重置表单
                          $("#inputForm")[0].reset();  
                          $(saveForm.t).attr("disabled",false);
                          // 刷新页面
                          window.parent.frames[0].searchListForm();
                          addItemValue();
                          $("#showHtml").removeClass("showHtml");
                          $("#confirmPage").css("display","none");
                          $("#editPage").css("display","block");
                          $("#showSubItemConfirm").html("&nbsp;");
                          $("#showSubDiv").show();
                      }else{
                          // 刷新页面
                          window.parent.frames[0].searchListForm();
                          closeWin(); 
                      }
                  }else{
                      $(saveForm.t).attr("disabled",false);
                  }
              },'json'); 
          }
      }

      // 删除子项目
      function deleteSub(delIndex){
          // 调用后台删除子项方法
          $.post("${ctx}/material/category/deleteSub?delIndex=" + delIndex, "", function(result) {
              // 回调函数赋值
              var h = $(callBackHtml(result)).find("#subItem").html();
              $('#subItem').html(h);
          }, 'html');
      }

      // 新建子项
      function createSubItem(delIndex) {
          // 项目值不能为空
          if ($('#itemValue').val() != null 
              && $('#itemValue').val() != 'null' 
              && $('#itemValue').val().trim() != '' 
              && $('#itemCode').val().trim() != $('#itemValue').val().trim() != ''
              &&lettersnumbers.test($("#itemValue").val())) {

                  popup('800px','519px','${ctx}/material/category/formSub?itemCode=' + $('#itemValue').val()+"&delIndex="+delIndex);

          } else {

              // 请输入合法项目值！
          layer.msg('<spring:message code="Please_enter_the_legal_project_value"/>', {time:1000});

          }

      }
      
      //选择添加子项目
      function addItemValue(){
          // 调用后台查询子项list
          $.post("${ctx}/material/category/searchSubList?itemValue = " + $('#itemValue').val(), "", function(result) {
              // 回调函数赋值
              var h = $(callBackHtml(result)).find("#subItem").html();
              $('#subItem').html(h);
          }, 'html');
          
            
      }
      
      // 子项值是否重复
      function isSubItemValueRepeate(itemValue,delIndex) {

          var number = 0;
          // 页面获取值比较
          $("[name='subItemValues']").each(function(i){
              if (itemValue.trim() == $(this).val().trim()&& delIndex!=i) {
                  number = number+1;
            }
          });
          
         if (number> 0) {
             return true;
         } else {
             return false;
         }
        
      }
    </script>
    </head>
	<body>
	<div id="editPage" class="layerHeader clearfix">
	      <c:choose>
               <c:when test="${category.id!=null&&category.id!=''}">
                   <!--编辑-->
                   <p class="headerTitle"><spring:message code="category_modifyCategory"/></p>
               </c:when>
               <c:otherwise>
                   <!--新建-->
                   <p class="headerTitle"><spring:message code="category_createCategory"/></p>
               </c:otherwise>
           </c:choose>
	    <div class="headerButton clearfix">
	        <shiro:hasPermission name="material:category:edit">
	        <button type="button" id="confirmBtn" onclick="checkForm.check(this)"><spring:message code="btn_ok"/></button>
	        </shiro:hasPermission>
	        <button type="button" onclick="closeWin()"><spring:message code="btn_cancle"/></button>
	    </div>
	</div>
	<form id="inputForm" method="post">
    <input type="hidden" id="id" name="id" value="${category.id}"/>
    <input type="hidden"  name="version" value="${category.version}"/>
    <input type="hidden"  id="listLength" value="${fn:length(categoryList) } "/>
    <input type="hidden" id="itemCode" name="itemCode" value="${category.itemCode}"/>
    <input type="hidden" id="oldItemValue" name="oldItemValue" value="${category.itemValue}"/>
    
    <!-- 确认页面head部 -->
	<div id="confirmPage" class="layerHeader clearfix" style="display:none">
		<p class="headerTitle"><spring:message code="category_head_confirm_material"/></p>
	    <div class="headerButton clearfix">
	        <shiro:hasPermission name="material:category:edit">
	        <button type="button" id="saveBtn" onclick="saveForm.save(0,this)"><spring:message code="category_save"/></button>
	        <c:if test="${empty category.id}">
	            <button type="button" id="saveContBtn" onclick="saveForm.save(1,this)"><spring:message code="category_saveAndContinue"/></button>
	        </c:if>
	        </shiro:hasPermission>
	        <button type="button" id="backBtn"><spring:message code="category_btn_back"/></button>
	    </div>
	</div>
    
    <div id="showHtml">
	<div class="smOrderBox">
	    <div class="items clearfix">
	        <!-- 物料分类代码 -->
	        <div class="col-50 clearfix">
               <!-- 分类代码 -->
               <p class="tit"><spring:message code="category_itemCode"/></p>
               <p>${category.itemCode}</p> 
	        </div>
	        <!-- 分类值 -->
	        <div class="col-50 clearfix haveToBox">
              <p class="tit"><spring:message code="category_itemValue"/></p>
                  <input type="text"  id="itemValue" name="itemValue" value="${category.itemValue}" data-rule-notEqualTo="#itemCode" data-msg-notEqualTo="<spring:message code="dictItemValueAndItmeCodeNotRepetition"/>" data-rule-lettersnumbers="true" data-msg-lettersnumbers="<spring:message code="category_pleaseInputItemValue"/>" maxlength="21" data-rule-required="true" data-msg-required="<spring:message code="category_pleaseInputItemValue"/>">
              <p class="haveTo">*</p>
	        </div>
	    </div>
	    <div class="items clearfix haveToBox">
	        <!-- 中文全称 -->
	        <div class="col-50 clearfix haveToBox">
                <p class="tit"><spring:message code="category_show_subCn"/></p>
                    <input type="text"  id="itemNameCn" name="itemNameCn" value="${category.itemNameCn}"  maxlength="50" data-rule-required="true"   data-msg-required="<spring:message code="category_pleaseInputCn"/>">
                <p class="haveTo">*</p>
	        </div>
	        <!-- 英文全称 -->
	        <div class="col-50 clearfix haveToBox">
               <p class="tit"><spring:message code="category_show_subEn"/></p>
                   <input type="text"  id="itemNameEn" name="itemNameEn" value="${category.itemNameEn}"  maxlength="50" data-rule-required="true"  data-msg-required="<spring:message code="category_pleaseInputEn"/>">
               <p class="haveTo">*</p>
	        </div>
	    </div>
	</div>
	<div class="smOrderBox" id="subItem">
		<div id="showSubDiv" class="items clearfix">
		<p id="showSubItemEdit" class="tit"><spring:message code="category_show_sub"/></p>
		<button type="button" onclick="createSubItem(-1)"><spring:message code="category_chooseAdd"/></button>
		</div>
		<div class="items clearfix">	
		<p id="showSubItemConfirm" class="tit">&nbsp;</p>
		<div class="tableArea">
	    <!-- 子项目 -->
	    <div style = "overflow-y:auto;border-top:none;padding-top:36px;height:217px;">
	        <table cellpadding="0" class="tableHead">
	            <thead>
	             <tr>
	                 <!-- 项目值 -->
	                 <th><p class="width-185px"><spring:message code="category_show_subItemValue"/></p></th>
	                 <!-- 中文全称 -->
	                 <th><p class="width-250px"><spring:message code="category_show_subCn"/></p></th>
	                 <!-- 英文全称 -->
	                 <th><p class="width-250px"><spring:message code="category_show_subEn"/></p></th>
	                 <!-- 子项目删除 -->
                     <th><p class="width-95px"><spring:message code="category_show_subDelete"/></p></th>
	             </tr>
	            </thead>
	         </table>
	         <table cellpadding="0">   
	            <tbody>
                        <c:forEach items="${categoryList}" var="categoryList" varStatus="status">
                            <tr>
                                <td><p class="width-185px">${categoryList.itemValue}<input name="subItemValues" type="hidden" value="${categoryList.itemValue}"/></p></td>
                                <td><p class="width-250px">${categoryList.itemNameCn}</p></td>
                                <td><p class="width-250px">${categoryList.itemNameEn}</p></td>
                                <td class="width-100px">
                                    <img class="delBtn" src="${ctxStatic}/images/btn-bj.png" onclick="createSubItem(${status.index})">
                                    <img class="delBtn" src="${ctxStatic}/images/btn-sc.png"  onclick="deleteSub(${status.index})">
                                </td>
                            </tr>
                        </c:forEach>
	            </tbody>
	        </table>
	    </div>
		</div>
		</div>
	</div>	
    <div class="smOrderBox">
	    <div class="items clearfix">
	        <!-- 状态 -->
	        <div class="col-50 clearfix labelBox">
	           <p class="tit"><spring:message code="category_state"/></p>
               <label>
               <input type="radio" name="enableFlag" checked='checked' value="1" <c:if test="${category.enableFlag eq 1}">checked="checked"</c:if> class="rast"/><spring:message code="category_enable"/>
               </label>
               <label>
                   <input type="radio" name="enableFlag" value="0" <c:if test="${category.enableFlag eq 0}">checked="checked"</c:if> class="rast" /><spring:message code="category_disable"/>
               </label>
	        </div>
	        <!-- 描述 -->
	        <div class="col-50 clearfix">
                <p class="tit"><spring:message code="category_remarks"/>
                <span class="txtRg">(0/100)</span>
                </p>
                <textarea style="width:360px" name="remarks"  maxlength="100" value="${category.remarks}">${category.remarks}</textarea>
	        </div>
	    </div>
	</div>
	</div>
	</form>
	</body>
</html>